<template>
  <el-container>
    <el-header>
      <img src="../assets/images/logo.png" alt="" />
      <el-menu
        class="el-menu-demo"
        mode="horizontal"
        background-color="#23262e"
        text-color="#fff"
        active-text-color="#ffd04b"
      >
        <el-submenu index="1">
          <template slot="title">
            <img
              :src="$store.state.user.user.user_pic"
              alt=""
              v-if="$store.state.user.user.user_pic"
              class="abc"
            />
            <img src="../assets/logo.png" alt="" class="abc" v-else />个人中心</template
          >
          <el-menu-item index="1-1"><i class="el-icon-edit"></i>用户资料</el-menu-item>
          <el-menu-item index="1-2"><i class="el-icon-edit"></i>重置密码</el-menu-item>
          <el-menu-item index="1-3"><i class="el-icon-edit"></i>更换头像</el-menu-item>
        </el-submenu>
        <el-menu-item index="1" @click="logout"
          ><i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu
          class="el-menu-demo"
          background-color="#23262e"
          text-color="#fff"
          router
          unique-opened=""
        >
          <div class="userinfo">
            <img
              :src="$store.state.user.user.user_pic"
              alt=""
              v-if="$store.state.user.user.user_pic"
              class="abc"
            />
            <img src="../assets/logo.png" class="abc" alt="" v-else />
            <p>
              欢迎你<span>{{
                $store.state.user.user.nickname || $store.state.user.user.username
              }}</span>
            </p>
          </div>
          <template v-for="item in menu">
            <el-menu-item
              :index="item.indexPath"
              v-if="item.children === null"
              :key="item.indexPath"
              ><i :class="item.icon"></i>{{ item.title }}</el-menu-item
            >
            <el-submenu :index="item.indexPath" :key="item.indexPath" v-else>
              <template slot="title"><i :class="item.icon"></i>{{ item.title }}</template>
              <template v-for="ites in item.children">
                <el-menu-item :index="ites.indexPath" :key="ites.indexPath"
                  ><i :class="ites.icon"></i>{{ ites.title }}
                </el-menu-item>
              </template>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenuAPI } from '@/api/menu'
export default {
  name: 'MyHome',
  data() {
    return {
      menu: []
    }
  },
  created() {
    this.$store.dispatch('user/getUser')
    this.getmenu()
  },
  methods: {
    logout() {
      this.$confirm('你确定要弃我而去吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$store.commit('user/updataUser', {})
          this.$store.commit('user/uptoken', '')
          this.$router.push('/login')
        })
        .catch(() => {})
    },
    async getmenu() {
      const { data: res } = await getMenuAPI()
      console.log(res)
      this.menu = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  background-color: #23262e;
  .el-menu-demo {
    display: flex;
    font-size: 14px;
  }
}
.el-aside {
  background-color: #23262e;
  .userinfo {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 10px;
    vertical-align: middle;
    color: #fff;
    font-size: 10px;
  }
}
.el-container {
  height: 100%;
}
.el-menu-item {
  font-size: 13px;
}

.abc {
  width: 30px;
  height: 30px;
  margin-right: 10px;
  vertical-align: middle;
  object-fit: cover;
  border-radius: 50%;
  background-color: #fff;
}
</style>
